﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data.SqlClient;
using AspNetCs.Config;
using System.Data;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class SaleNumber_DAL : ISaleNumber
    {




        #region ISaleNumber 成员

        public int add(SaleNumber_Model model)
        {
            db.ParameterArry[] par = new db.ParameterArry[10];
            par[0].dbType = SqlDbType.Int;
            par[0].objValue = model.SaleType;
            par[0].parName = "@SaleType";

            par[2].dbType = SqlDbType.VarChar;
            par[2].objValue = model.Number;
            par[2].parName = "@Number";


            par[1].dbType = SqlDbType.Decimal;
            par[1].objValue = model.MemberPrice;
            par[1].parName = "@MemberPrice";


            par[3].dbType = SqlDbType.Decimal;
            par[3].objValue = model.AgentPrice;
            par[3].parName = "@AgentPrice";

            par[4].dbType = SqlDbType.Int;
            par[4].objValue = model.ReCommend;
            par[4].parName = "@ReCommend";

            par[5].dbType = SqlDbType.Int;
            par[5].objValue = model.UserId;
            par[5].parName = "@UserId";

            par[6].dbType = SqlDbType.Int;
            par[6].objValue = model.AgentID;
            par[6].parName = "@AgentID";

            par[7].dbType = SqlDbType.Int;
            par[7].objValue = model.AgentState;
            par[7].parName = "@AgentState";

            par[8].dbType = SqlDbType.Int;
            par[8].objValue = model.UserState;
            par[8].parName = "@UserState";


            par[9].dbType = SqlDbType.DateTime;
            par[9].objValue = DateTime.Now;
            par[9].parName = "@Addtime";



            int Result = -1;
            db Conn = new db();
            try
            {
                string Sql = "Insert into SaleNumber(Number,SaleType,MemberPrice,AgentPrice,ReCommend,UserId,AgentID,AgentState,UserState) values(@Number,@SaleType,@MemberPrice,@AgentPrice,@ReCommend,@UserId,@AgentID,@AgentState,@UserState)";
                Result = Conn.ExcSql(Sql, par);
            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }
            finally
            {
                Conn.close();
            }
            return Result;
        }

        public SaleNumber_Model GetModel(DataRow dr)
        {
            SaleNumber_Model model = new SaleNumber_Model();
            if (dr["ID"] != null) { model.Id = StringHelper.isNumber_int(dr["id"].ToString()); }
            if (dr["Number"] != null) { model.Number = dr["Number"].ToString(); }
            if (dr["SaleType"] != null) { model.SaleType = StringHelper.isNumber_int(dr["SaleType"].ToString()); }
            if (dr["MemberPrice"] != null) { model.MemberPrice = StringHelper.isNumber_decimal(dr["MemberPrice"].ToString()); }

            if (dr["AgentPrice"] != null) { model.AgentPrice = StringHelper.isNumber_decimal(dr["AgentPrice"].ToString()); }
            if (dr["ReCommend"] != null) { model.ReCommend = StringHelper.isNumber_int(dr["ReCommend"].ToString()); }
            if (dr["UserId"] != null) { model.UserId = StringHelper.isNumber_int(dr["UserId"].ToString()); }
            if (dr["AgentID"] != null) { model.AgentID = StringHelper.isNumber_int(dr["AgentID"].ToString()); }
            if (dr["AgentState"] != null) { model.AgentState = StringHelper.isNumber_int(dr["AgentState"].ToString()); }
            if (dr["UserState"] != null) { model.UserState = StringHelper.isNumber_int(dr["UserState"].ToString()); }
            if (dr["Addtime"] != null) { model.Addtime = dr["Addtime"].ToString(); }
            return model;
        }

        public SaleNumber_Model GetModel(int id)
        {
            SaleNumber_Model model = new SaleNumber_Model();
            db ConnModel = new db();
            DataTable dt = ConnModel.GetSql_DataTable("Select * from SaleNumber Where id=" + id);
            foreach (DataRow dr in dt.Rows)
            {
                model = GetModel(dr);
            }
            return model;
        }
        /// <summary>
        /// 查询馆号是否已经存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int NumberIsTrue(SaleNumber_Model model)
        {
            int result = 0;
            db Conn = new db();
            try
            {
                db.ParameterArry[] par = new db.ParameterArry[2];
                par[0].dbType = SqlDbType.Int;
                par[0].objValue = model.SaleType;
                par[0].parName = "@SaleType";

                par[1].dbType = SqlDbType.VarChar;
                par[1].objValue = model.Number;
                par[1].parName = "@Number";
                string Sqlstr = "Select Count(id) From SaleNumber where SaleType=@SaleType and Number=@Number";
                result = (int)Conn.GetOneFile(Sqlstr, par);
            }
            catch
            {

                result = -1;
            }
            finally
            {
                Conn.close();
            }
            return result;
        }

        /// <summary>
        /// 根据条件,查询馆号. 返回DATATBALE
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public DataTable GetDataTable(string filter)
        {
            db Conn = new db();
            DataTable dt = new DataTable();
            try
            {
                dt = Conn.GetSql_DataTable("Select  * from SaleNumber where id<>0 " + filter);
            }
            catch (Exception ex)
            {
                new CustomException(ex);
            }
            finally
            {
                Conn.close();
            }
            return dt;
        }

        #endregion

        /// <summary>
        /// 检查公墓编号是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataTable GetNetMuIdDataTable(string filter)
        {
            db Conn = new db();
            DataTable dt = new DataTable();
            try
            {
                dt = Conn.GetSql_DataTable("Select  * from SaleNumber where id<>0 " + filter);
            }
            catch (Exception ex)
            {
                new CustomException(ex);
            }
            finally
            {
                Conn.close();
            }
            return dt;
        }

        /// <summary>
        /// 检查宗祠编号是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataTable GetAncestralHallIdDataTable(string filter)
        {
            db Conn = new db();
            DataTable dt = new DataTable();
            try
            {
                dt = Conn.GetSql_DataTable("Select  * from SaleNumber where id<>0 " + filter);
            }
            catch (Exception ex)
            {
                new CustomException(ex);
            }
            finally
            {
                Conn.close();
            }
            return dt;
        }
    }
}
